//
//  SpecialViewController.m
//  悠狸
//
//  Created by dlios on 15-8-30.
//  Copyright (c) 2015年 Dung. All rights reserved.
//

#import "SpecialViewController.h"
#import "Webview.h"
#import "Asyn.h"
#import "Tools.h"

#import "MyModel.h"
#import "URL.h"
#import "SpecialTableViewCell.h"
#import "SpecialDetailsViewController.h"
@interface SpecialViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UITableView *table;
@property (nonatomic, retain)MyModel *model;
@end

@implementation SpecialViewController

- (void)dealloc
{
   
    [_model release];
    [_array release];
    [_table release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"全部专题";
    self.navigationController.navigationBar.translucent = NO;
    [self create];
    [self parser];
}
//http://api.liwushuo.com/v2/collections?limit=10&offset=0
- (void)parser
{
    NSMutableDictionary *dic =[ NSMutableDictionary dictionary];
    [dic setValue:@"10" forKey:@"limit"];
    [dic setValue:@"0" forKey:@"offset"];
    
    
    [Asyn startParser:QBZT_QBZT partemer:dic file:@"data9" block:^(NSData *data) {
        //存储
        [Tools toolsOfSaveLibraryPathName:@"data9" data:data];
        NSDictionary *dic = [Tools toolsOfJsonDataType:dicType fileName:@"data9"];


        NSDictionary *dic1 = [dic objectForKey:@"data"];
        NSArray *arr = [dic1 objectForKey:@"collections"];
        for (NSDictionary *dic2 in arr) {
            MyModel *model = [[[MyModel alloc] init] autorelease];
            [model setValuesForKeysWithDictionary:dic2];
            [_array addObject:model];
            
        }
        [_table reloadData];
    }];
    
    
}
- (void)create
{
    
    self.table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain] autorelease];
    _table.rowHeight = 200;
    _table.delegate = self;
    _table.dataSource = self;
    
    [self.view addSubview:_table];
    
    [_table registerClass:[SpecialTableViewCell class] forCellReuseIdentifier:@"i"];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"i"];
    
    MyModel *model = [_array objectAtIndex:indexPath.row];
    self.model = model;
    [cell setmodel:model];
  
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    SpecialDetailsViewController *special = [[[SpecialDetailsViewController alloc]init ] autorelease];
#pragma mark_____切记方法
    special.model = [_array objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:special animated:YES];
    
}
@end
